﻿using Abp.Application.Services;
using Abp.AspNetCore.Mvc.Authorization;
using Abp.AspNetZeroCore.Net;
using Abp.Auditing;
using Abp.Authorization;
using AbpZeroTemplate.Authorization.Users.Dto;
using AbpZeroTemplate.Authorization.Users.Profile;
using AbpZeroTemplate.Authorization.Users.Profile.Dto;
using AbpZeroTemplate.Storage;
using AbpZeroTemplate.Web.Controllers;
using AbpZeroTemplate.Web.Swagger;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;

namespace AbpZeroTemplate.Api.Admin.Controllers
{
    /// <summary>头像
    /// </summary>
    [Area("Admin")]
    [Route("Admin/Profile/[action]")]
    [ApiName("Admin")]
    [RemoteService]
    [AbpMvcAuthorize]
    public class ProfileController : AbpZeroTemplateControllerBase
    {
        private readonly IProfileAppService _profileAppService;
        private readonly IBinaryObjectManager _binaryObjectManager;
        /// <summary>
        /// </summary>
        public ProfileController(IProfileAppService profileAppService, IBinaryObjectManager binaryObjectManager)
        {
            _profileAppService = profileAppService;
            _binaryObjectManager = binaryObjectManager;
        }

        [HttpGet]
        [DisableAuditing]
        public async Task<CurrentUserProfileEditDto> GetCurrentUserProfileForEdit()
        {
            return await _profileAppService.GetCurrentUserProfileForEdit();
        }


        [HttpPost]
        public async Task<UpdateGoogleAuthenticatorKeyOutput> UpdateGoogleAuthenticatorKey()
        {
            return await _profileAppService.UpdateGoogleAuthenticatorKey();
        }

        [HttpPost]
        public async Task SendVerificationSms()
        {
            await _profileAppService.SendVerificationSms();
        }

        [HttpPost]
        public async Task VerifySmsCode([FromBody]VerifySmsCodeInputDto input)
        {
            await _profileAppService.VerifySmsCode(input);
        }

        [HttpPost]
        public async Task UpdateCurrentUserProfile([FromBody]CurrentUserProfileEditDto input)
        {
            await _profileAppService.UpdateCurrentUserProfile(input);
        }

        /// <summary>修改密码
        /// </summary>
        [HttpPost]
        public async Task ChangePassword([FromBody]ChangePasswordInput input)
        {
            await _profileAppService.ChangePassword(input);
        }

        /// <summary>修改个人头像
        /// </summary>
        [HttpPut]
        public async Task UpdateProfilePicture([FromBody]UpdateProfilePictureInput input)
        {
            await _profileAppService.UpdateProfilePicture(input);
        }

        /// <summary>获取密码设置
        /// </summary>
        [HttpGet]
        [AbpAllowAnonymous]
        [DisableAuditing]
        public async Task<GetPasswordComplexitySettingOutput> GetPasswordComplexitySetting()
        {
            return await _profileAppService.GetPasswordComplexitySetting();
        }

        /// <summary>获取头像图片
        /// </summary>
        [HttpGet]
        [DisableAuditing]
        public async Task<GetProfilePictureOutput> GetProfilePicture()
        {
            return await _profileAppService.GetProfilePicture();
            //var output = await _profileAppService.GetProfilePicture();
            //if (output.ProfilePicture.IsNullOrWhiteSpace())
            //{
            //    return GetDefaultProfilePicture();
            //}
            //return await GetProfilePictureByGuid(Guid.Parse(output.ProfilePicture));
        }

        [HttpGet]
        public async Task<GetProfilePictureOutput> GetFriendProfilePictureById(GetFriendProfilePictureByIdInput input)
        {
            return await _profileAppService.GetFriendProfilePictureById(input);
        }

        /// <summary>获取好友头像
        /// </summary>
        //[HttpGet]
        //public async Task<FileResult> GetFriendProfilePictureById(GetFriendProfilePictureByIdInput input)
        //{
        //    var output = await _profileAppService.GetFriendProfilePictureById(input);
        //    return await GetProfilePictureByGuid(Guid.Parse(output.ProfilePicture));
        //}

        //[HttpGet]
        //public async Task<GetProfilePictureOutput> GetProfilePictureById(Guid profilePictureId)
        //{
        //    return await _profileAppService.GetProfilePictureById(profilePictureId);
        //}

        [HttpPost]
        public async Task ChangeLanguage([FromBody]ChangeUserLanguageDto input)
        {
            await _profileAppService.ChangeLanguage(input);
        }


        /// <summary>根据Guid获取头像
        /// </summary>
        [HttpGet]
        public async Task<FileResult> GetProfilePictureById(Guid id)
        {
            return await GetProfilePictureByGuid(id);
        }

        /// <summary>获取默认头像
        /// </summary>
        private FileResult GetDefaultProfilePicture()
        {
            return File(
                @"Common\Images\default-profile-picture.png",
                MimeTypeNames.ImagePng
            );
        }
        private async Task<FileResult> GetProfilePictureByGuid(Guid profilePictureId)
        {
            var file = await _binaryObjectManager.GetOrNullAsync(profilePictureId);
            if (file == null)
            {
                return GetDefaultProfilePicture();
            }

            return File(file.Bytes, MimeTypeNames.ImageJpeg);
        }
    }
}
